package com.hcbd.tdwt.ui.setting.xingyu.activity;
import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.base.bd.XYSerialPortManage;
import com.hcbd.tdwt.util.SharedPreferencesUtil;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_send_msg_set)
public class SendMsgSettingActivity extends BaseActivity {
    private String TAG = "SendMsgSettingActivity";
    @ViewById
    Button saveBtn;
    @ViewById(R.id.chinese_character_mode)
    RadioButton chineseRb;
    @ViewById(R.id.code_pattern)
    RadioButton codePatternRb;
    @ViewById(R.id.blend_mode)
    RadioButton blendModeRb;
    @ViewById
    RadioGroup modeRG,command_group;
    @ViewById
    RadioButton command_high,command_middle,command_wrap,command_custom;
    @ViewById
    EditText custom_ed;
    @ViewById
    LinearLayout command_ll2,command_ll3;
    SharedPreferencesUtil sp;
    String command = "";  // 需要下发的指令
    int index = -1;
    private final String RATE_HIGH = "CCUDF,0,16,16,16,16";
    private final String RATE_MIDDLE = "CCUDF,0,10,10,16,16";
    private final String RATE_WRAP = "CCUDF,0,6,8,10,16";
    private int clickCount = 0;
    private long lastClickTime = 0;
    @AfterViews
    protected void init() {
        chineseRb.setTag(Constant.CHINESE_CHAR_MODE);
        codePatternRb.setTag(Constant.CODE_PATTERN);
        blendModeRb.setTag(Constant.BLEND_MODE);
        sp=new SharedPreferencesUtil(this);
        String mode=sp.getAttribute(Constant.MSG_MODE);
        if(Constant.CHINESE_CHAR_MODE.equals(mode)){
            chineseRb.setChecked(true);
        }else if(Constant.BLEND_MODE.equals(mode)){
            blendModeRb.setChecked(true);
        }else{
            codePatternRb.setChecked(true);
        }

        int power = sp.getAttributeInt(Constant.POWER_MODE);
        index = power;
        if(power==-1){
            command_high.setChecked(true);
            command = RATE_HIGH;
        }else if(power==1){
            command_middle.setChecked(true);
            command = RATE_MIDDLE;
        }else if(power==2){
            command_wrap.setChecked(true);
            command = RATE_WRAP;
        }else {
            command_custom.setChecked(true);
            command = sp.getAttribute(Constant.CUSTOM_POWER_MODE);
            custom_ed.setText(command);
            custom_ed.setVisibility(View.VISIBLE);
        }
        command_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==R.id.command_high){
                    custom_ed.setVisibility(View.INVISIBLE);
                    command = RATE_HIGH;
                    index = -1;
                }else if(i==R.id.command_middle){
                    custom_ed.setVisibility(View.INVISIBLE);
                    command = RATE_MIDDLE;
                    index = 1;
                }else if(i==R.id.command_wrap){
                    custom_ed.setVisibility(View.INVISIBLE);
                    command = RATE_WRAP;
                    index = 2;
                }else if(i==R.id.command_custom){
                    command = "";
                    custom_ed.setVisibility(View.VISIBLE);
                    index = 3;
                }
            }
        });

        command_ll3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "onClick: 点击" );
                if(command_ll2.getVisibility()==View.VISIBLE){
                    return;
                }
                else {
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - lastClickTime < 1000) {
                        clickCount++; // 点击计数加一
                        if (clickCount >= 3) {
                            // 连续点击按钮三次且每次点击间隔小于1秒
                            command_ll2.setVisibility(View.VISIBLE);
                            command_ll3.setVisibility(View.GONE);
                            clickCount = 0; // 重置点击计数
                        }
                    } else {
                        clickCount = 1; // 重置点击计数
                    }
                    lastClickTime = currentTime;
                }
            }
        });

    }
    @Click
    void saveBtn(){
        RadioButton rb = findViewById(modeRG.getCheckedRadioButtonId());
        sp.addAttribute(Constant.MSG_MODE,rb.getTag().toString());
        if(command_ll2.getVisibility()==View.VISIBLE){
            if(!BdToolFactory.getBdSendBase().enableRD){
                toast("请先打开北斗模块");
                return;
            }
            if(index==3){
                String cus_com = custom_ed.getText().toString();
                if(cus_com.equals("")){
                    toast("请输入自定义指令");
                    return;
                }
                command = cus_com;
                sp.addAttribute(Constant.CUSTOM_POWER_MODE,command);
            }
            XYSerialPortManage.getInstance().sendCustomCommand(command);
            sp.addAttribute(Constant.POWER_MODE,index);
            Log.e(TAG, "下发指令："+command);
        }
        finish();
        toast("保存成功！");
    }

    @Click
    void headLeft(){
        finish();
    }

}
